//
//  TLReachability.m
//  KidReading
//
//  Created by telen on 16/5/6.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "TLReachability.h"
#import "JRReachability.h"

@interface TLReachability()
@property (nonatomic) JRReachability *internetReachability;
@property (nonatomic) JRReachability *wifiReachability;
@end

@implementation TLReachability
{
    NSMutableArray* noticeDelegateArr;
    
}

+ (instancetype)shareInstance
{
    static TLReachability* tlReachabilitySingle = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tlReachabilitySingle = [[TLReachability alloc] init];
    });
    return tlReachabilitySingle;
}

- (instancetype)init
{
    if (self = [super init]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
        noticeDelegateArr = [NSMutableArray new];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}

- (void)reachabilityChanged:(NSNotification *)note
{
    JRReachability* curReach = [note object];
    if ([curReach isKindOfClass:[JRReachability class]]) {
        [self updateInterfaceWithReachability:curReach];
        NSArray* arr = [NSArray arrayWithArray:noticeDelegateArr];
        for (NSValue* value in arr) {
            id<TLReachabilityDelegate> delegate = [value nonretainedObjectValue];
            if(delegate && [delegate respondsToSelector:@selector(tlReachabilityNetStatusChanged:)]){
                [delegate tlReachabilityNetStatusChanged:self];
            }
        }
    }
}

- (void)updateInterfaceWithReachability:(JRReachability *)reachability
{
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    //BOOL connectionRequired = [reachability connectionRequired];
    if (reachability == self.internetReachability)
    {
        _netStatus = netStatus!=NotReachable;
    }
    
    if (reachability == self.wifiReachability)
    {
        _wifiStatus = netStatus!=NotReachable;
    }
    
    //NSLog(@"net %@ -- wifi %@",@(_netStatus),@(_wifiStatus));
}

- (void)startNetStatusNotifier
{
    if (!self.internetReachability) {
        self.internetReachability = [JRReachability reachabilityForInternetConnection];
        [self.internetReachability startNotifier];
        [self updateInterfaceWithReachability:self.internetReachability];
    }
    
    if (!self.wifiReachability) {
        self.wifiReachability = [JRReachability reachabilityForLocalWiFi];
        [self.wifiReachability startNotifier];
        [self updateInterfaceWithReachability:self.wifiReachability];
    }
}

- (void)stopNetStatusNotifier
{
    if (self.internetReachability) {
        [self.internetReachability stopNotifier];
        self.internetReachability = nil;
    }
    
    if (!self.wifiReachability) {
        [self.wifiReachability stopNotifier];
        self.wifiReachability = nil;
    }
}

- (void)registerNotice_delegate:(id<TLReachabilityDelegate>)delegate
{
    NSValue* value = [NSValue valueWithNonretainedObject:delegate];
    [noticeDelegateArr addObject:value];
}

- (void)unRegisterNotice_delegate:(id<TLReachabilityDelegate>)delegate
{
    NSArray* arr = [NSArray arrayWithArray:noticeDelegateArr];
    for (NSValue* value in arr) {
        if([value nonretainedObjectValue] == delegate){
            [noticeDelegateArr removeObject:value];
        }
    }
}

@end
